<template>
  <div class="container">
    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 标题区域 -->
      <div class="login-header">
        <div class="title">自习室管理系统</div>
        <div class="subtitle">管理员登录入口</div>
      </div>

      <!-- 表单区域 -->
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input
              prefix-icon="el-icon-user"
              placeholder="请输入管理员账号"
              v-model="form.username"
              class="input-field"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              prefix-icon="el-icon-lock"
              placeholder="请输入管理员密码"
              show-password
              v-model="form.password"
              class="input-field"
          ></el-input>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
              class="login-btn"
              @click="login"
          >
            管理员登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: '',
        password: '',
        role: 'ADMIN'
      },
      rules: {
        username: [
          { required: true, message: '请输入管理员账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入管理员密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过，发送登录请求
          this.$request.post('/login', this.form).then(res => {
            if (res.code === '200') {
              localStorage.setItem("xm-user", JSON.stringify(res.data))
              this.$router.push('/')
              this.$message.success('登录成功')
            } else {
              this.$message.error(res.msg || '登录失败')
            }
          }).catch(err => {
            this.$message.error('网络错误')
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  background: #f9f7ff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 350px;
  padding: 40px 30px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(123, 97, 255, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 22px;
  font-weight: bold;
  color: #5d46c3;
  margin-bottom: 5px;
}

.subtitle {
  font-size: 14px;
  color: #9a89e0;
}

.input-field {
  border-radius: 4px;
  border-color: #e0d9f7;
}

.input-field:focus {
  border-color: #7b61ff;
}

.login-btn {
  width: 100%;
  background-color: #7b61ff;
  border-color: #7b61ff;
  color: white;
}

.login-btn:hover {
  background-color: #6a54e0;
  border-color: #6a54e0;
  color: white;
}
</style>
